<%@page import="cn.net.ecode.modules.sys.security.AuthorizingRealm"%>
<%@page import="cn.net.ecode.common.utils.SpringContextHolder"%>
<%@ page import="net.sf.ehcache.CacheManager" %>
<%@ page import="net.sf.ehcache.Statistics" %>
<%@ page import="java.util.Arrays" %>
<%@ page import="java.util.Comparator" %>
<%@ page contentType="text/html;charset=UTF-8" trimDirectiveWhitespaces="true"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
	<style>
		table,td{border:1px solid #ddd;border-collapse:collapse;padding:8px;}
	</style>
	<p>
		<a href="?sort=hitPercent">按总命中率排序</a> &nbsp;
		<a href="?sort=objectCount">按内存中对象数排序</a>
	</p>
	<table>
        <tr>
            <td>名称</td>
            <td>总命中率</td>
            <td>命中次数</td>
            <td>失效次数</td>
            <td>缓存总对象数</td>
            <td>最后一秒查询完成的执行数</td>
            <td>最后一次采样的平均执行时间</td>
            <td>平均获取时间</td>
        </tr>
<%
    CacheManager cacheManager = ((CacheManager)SpringContextHolder.getBean("cacheManager"));
    String[] cacheNames = cacheManager.getCacheNames();
    int length = cacheNames.length;

    sort(cacheNames, cacheManager, request.getParameter("sort"));

    for (int i = 0; i < length; i++) {
        String cacheName = cacheNames[i];
        if ((AuthorizingRealm.class.getName()+".authorizationCache").equals(cacheName)){
        	continue;
        }
        pageContext.setAttribute("cacheName", cacheName);
        pageContext.setAttribute("cache", cacheManager.getCache(cacheName));
%>
		<c:set var="totalCount" value="${cache.statistics.cacheHits + cache.statistics.cacheMisses}"/>
		<c:set var="totalCount" value="${totalCount > 0 ? totalCount : 1}"/>
		<c:set var="cacheHitPercent" value="${cache.statistics.cacheHits * 1.0 / (totalCount)}"/>
        <tr>
            <td><a href="info.jsp?cacheName=${cacheName}" target="_blank">${cacheName}</a></td>
            <td>${cacheHitPercent}</td>
            <td>${cache.statistics.cacheHits}</td>
            <td>${cache.statistics.cacheMisses}</td>
            <td>${cache.statistics.objectCount}</td>
            <td>${cache.statistics.searchesPerSecond}</td>
            <td>${cache.statistics.averageSearchTime}</td>
            <td>${cache.statistics.averageGetTime}毫秒</td>
        </tr>
<%
	}
%>
   	</table>
<%!
	private void sort(final String[] cacheNames, final CacheManager cacheManager, final String sort) {
	    Arrays.sort(cacheNames, new Comparator<String>() {
	        public int compare(String n1, String n2) {
	            Statistics s1 = cacheManager.getCache(n1).getStatistics();
	            Statistics s2 = cacheManager.getCache(n2).getStatistics();
	            if("hitCount".equals(sort)) {
	                double n1HitPercent = 1.0 * s1.getCacheHits() / Math.max(s1.getCacheHits() + s1.getCacheMisses(), 1);
	                double n2HitPercent = 1.0 * s2.getCacheHits() / Math.max(s2.getCacheHits() + s2.getCacheMisses(), 1);
	                return -Double.compare(n1HitPercent, n2HitPercent);
	            } else if("objectCount".equals(sort)) {
	                return -Long.valueOf(s1.getObjectCount()).compareTo(Long.valueOf(s2.getObjectCount()));
	            }
	            return -n1.compareTo(n2);
	        }
	
	    });
	}
%>